package com.joysuch.wwyt.common.repository;

import com.joysuch.wwyt.common.entity.CommonStaticsReport;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @Author ZhangDong
 * @Date 2020/7/23 14:14
 */
public interface CommonStaticsReportDao extends JpaRepository<CommonStaticsReport, Long>, JpaSpecificationExecutor<CommonStaticsReport> {

    @Query(value = "select 1 from wwyt_common_statics_report r where r.state=1 and r.title=:title " +
            "and if(:currentUserId is null,1=1,r.user_id=:currentUserId) and r.all_user_flag=:allUserFlag limit 1", nativeQuery = true)
    Integer existByTitle(String title, Long currentUserId, Integer allUserFlag);

    @Query("select r from CommonStaticsReport r where r.state=1 and r.userId=:userId and r.allUserFlag=0")
    List<CommonStaticsReport> queryByUserId(Long userId);

    @Query("select r from CommonStaticsReport r where r.state=1 and r.allUserFlag=1")
    List<CommonStaticsReport> queryByAllUser();

}
